--
-- Author: yawei.zhu
-- Date: 2018-09-05 17:34:32
--
-- PrestigeShopView
--

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)
local UniversalShopPanel = import(".UniversalShopPanel")
local AchView = require("app.system.achievement.views.AchView")
local TabsBtn = require("app.widget.TabsBtn")

ClassRef.VIEW_ROOT_RES = Res.PrestigeShopScene

function ClassRef:init()
    self:setNodeEventEnabled(true)
	self._ui.Button_close:addClickEventListener(function()
		executeFunc(self._onOperate, "closeUI")
    end)

    self._model = app:getInst("PrestigeShopModel")
    -- 初始化已开启声望商店列表(每次打开界面都重新初始化一次)
    self._model:initPrestigeShopList()
    self._shopPanel = UniversalShopPanel.create(self._ui.shop)

    -- TabsBtn
    self._curIndex = 1
    local btns = {}
    for i = 1, 3 do
        btns[i] = self._ui.Panel_tab:getChildByName("selected_btn_"..i)
    end
    local function onTabsBtn(index, btn)
        if not self._curIndex or self._curIndex ~= index then
            self._curIndex = index 
            self:_refreshViewByIndex(index)
        end
    end
    self._tabsBtn = TabsBtn.new(btns, onTabsBtn, 1)
end

function ClassRef:updatePanelInfo(data)
    if not data then return end
    local page, index = self._model:getIndexByShopId(data.shopId)
    if not cc.isDead(self._shopPanel) then
        -- 英雄徽章商店、光芒徽章商店、万能徽章商店不显示侧边Tab
        if data.shopId == 72061 or data.shopId == 72062 or data.shopId == 72063 then 
            self:setTabVisible(false)
            self._shopPanel:setShopId(data.shopId)
            self._shopPanel:setSelectItemInfo({data.itemType, data.itemId})
            self._shopPanel:updateLeftPanel()
        else
            local params = {
            shopId = data.shopId, 
            tabIndex = self._curIndex, 
            page = page, 
            index = index,
            itemType = data.itemType,
            itemId = data.itemId}
            self._shopPanel:initPanel(params)
        end
    end
end

function ClassRef:setTabVisible(b)
    self._ui.Panel_tab:setVisible(b)
end

function ClassRef:_refreshViewByIndex(index)
    if not index then return end
    if not cc.isDead(self._shopPanel) then
        self._shopPanel:setSelectedIndex(index)
    end
end

function ClassRef:updateGoldInfo()
    if not cc.isDead(self._shopPanel) then
        self._shopPanel:updateGoldInfo()
    end
end

function ClassRef:onOperate(cb)
    self._onOperate = cb
end

return ClassRef
